package cn.zhfeat.utils.redis;

import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPipeline;

import java.util.*;
import java.util.Map.Entry;

/**
 * @description:
 *
 * Redis对hash数据的操作
 * hash与数据库表结构类似 其中key可以对应为表名 field对应行主键 value对应行值
 *
 * @param
 * @author ZhangHao
 * @date 2018/10/18 11:30
 * @version 1.0.0.1
 * @return
 */

public class RedisHashUtil {
	//static ShardedJedis shardedJedis = null;

	/**
	 * 向名称为key的hash中添加元素field<—>value
	 * 
	 * 
	 * @param key
	 * @param field
	 * @param value
	 */
	public static void hset(String key, String field, String value) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		shardedJedis.hset(key, field, value);
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
	}

	/**
	 * 返回名称为key的hash中field对应的value
	 *
	 * @param key
	 * @param field
	 */
	public static String hget(String key, String field) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		String value=shardedJedis.hget(key, field);
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
		return value;
	}

	/**
	 * 向名称为key的hash中添加元素field i<—>value i
	 *
	 * @param key
	 * @param hash
	 */
	public static void hmset(String key, Map<String, String> hash) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		shardedJedis.hmset(key, hash);
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
	}

	/**
	 * 返回名称为key的hash中field i对应的value
	 *
	 * @param key
	 * @param fields
	 * @return
	 */
	public static List<String> hmget(String key, String[] fields) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		List<String> values=shardedJedis.hmget(key, fields);
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
		return values;
	}

	/**
	 * 查询名称为key的hash中所有的键
	 *
	 * @param key
	 * @return List<String>
	 */
	public static List<String> hkeys(String key) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		List<String> keys = new ArrayList<String>();
		Set<String> keysSet = shardedJedis.hkeys(key);
		keys.addAll(keysSet);
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
		return keys;
	}

	/**·
	 * 返回名称为key的hash中所有键对应的值value
	 *
	 * @param key
	 */
	public static List<String> hvals(String key) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		List<String> values = shardedJedis.hvals(key);
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
		return values;
	}

	/**
	 * 返回名称为key的hash中所有的键（field）及其对应的value
	 *
	 * @param key
	 */
	public static List<cn.zhfeat.utils.redis.RedisModel> hgetall(String key) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		Map<String, String> map = shardedJedis.hgetAll(key);
		Iterator<Entry<String, String>> iter = map.entrySet().iterator();
		List<cn.zhfeat.utils.redis.RedisModel> datas = new ArrayList<cn.zhfeat.utils.redis.RedisModel>();
		while (iter.hasNext()) {
			Entry<String, String> entry = iter.next();
			String key1 = entry.getKey();
			String value = entry.getValue();
			cn.zhfeat.utils.redis.RedisModel model = new cn.zhfeat.utils.redis.RedisModel();
			model.setKey(key1);
			model.setValue(value);
			datas.add(model);
		}
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
		return datas;
	}

	/**
	 * 将名称为key的hash中field的value增加integer
	 *
	 * @param key
	 * @param field
	 * @param integer
	 */
	public static Long hincrby(String key, String field, Long integer) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		Long result = shardedJedis.hincrBy(key, field, integer);
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
		return result;
	}

	/**
	 * 将名称为key的hash中field的value增加doubleValue
	 *
	 * @param key
	 * @param field
	 * @param Double
	 */
	public static Double hincrby(String key, String field, Double doubleValue) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		Double result = shardedJedis.hincrByFloat(key, field, doubleValue);
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
		return result;
	}

	/**
	 * 返回名称为key的hash中元素个数
	 *
	 * @param key
	 * @return
	 */
	public static long hlen(String key) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		Long result = shardedJedis.hlen(key);
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
		return result;

	}

	/**
	 * 名称为key的hash中是否存在键为field的域
	 *
	 * @param key
	 * @param field
	 * @return
	 */
	public static Boolean hexists(String key, String field) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		Boolean result = shardedJedis.hexists(key, field);
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
		return result;
	}

	/**
	 * 删除名称为key的hash中键为field的域
	 *
	 * @param key
	 * @param fields
	 * @return
	 */
	public static Long hdel(String key, String[] fields) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		long result = shardedJedis.hdel(key, fields);
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
		return result;
	}

	/**
	 * 插入大量数据
	 * @param key
	 * @param data 数据的封装  其中包括数据的key  数据的值value
	 */
	public static void setLargeData(String key, List<cn.zhfeat.utils.redis.RedisModel> data) {
		ShardedJedis shardedJedis = cn.zhfeat.utils.redis.RedisUtil.getShardedJedisObject();
		ShardedJedisPipeline pipeline = shardedJedis.pipelined();
		cn.zhfeat.utils.redis.RedisModel model = null;
		for (int i = 0; i < data.size(); i++) {
			model = data.get(i);
			pipeline.hset(key, model.getKey(), model.getValue());
		}
		pipeline.sync();
		cn.zhfeat.utils.redis.RedisUtil.recycleShardedJedisObject(shardedJedis);
	}
	
	
	public static void main(String[] args){
//		hset("person3", "13155822449", "康康");
//		System.out.println(hget("person3", "13155822449"));
		List<String> list=hvals("person3");
		System.out.println("数据大小----》"+list.size());
		
//		JSONObject jsonObject=JSONObject.parseObject(list.get(1));
//		System.out.println(jsonObject.toString());
		
		JSONObject object=JSONObject.parseObject(list.get(1));
		String name=object.getString("name");
		System.out.println(name);
	}
	
	
	
}
